package com.zhy.sping.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.event.ProgressListener;
import com.aliyun.oss.model.Callback;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: IdlerApi
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-07-05 16:48
 */
public class AliOssUtil {
    //平台的密钥 不要公开到公众平台
    private static final String accessKeyId="LTAI5t8QA19CRR2V15oHyHW8";//
    private static final String accessKeySecret="OqUKmRFUnAYm0frzUc43uKnk5BNoaP";//
    public static final String bucket="data213";
    private static final String endpoint="oss-cn-beijing.aliyuncs.com";
    private static OSS client;
    static {
        client=new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }
    /**
     * 上传文件*/
    public static String upload(String objName, InputStream is){
        //上传文件到云存储中心
        client.putObject(bucket,objName,is);
        //生成访问地址
        return createUrl(objName);
    }
    /**
     * 上传文件
     * 并实时获取上传的进度*/
    public static String upload(String objName, InputStream is, ProgressListener listener){
        //上传文件到云存储中心
        client.putObject(new PutObjectRequest(bucket,objName,is).withProgressListener(listener));
        //生成访问地址
        return createUrl(objName);
    }
    /**
     * 上传文件,并设置回调
     * @param cburl 回调接口的url地址*/
    public static String upload(String objName, InputStream is,String cburl){
        //上传文件到云存储中心
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, objName,is);
        Callback callback = new Callback();
        callback.setCallbackUrl(cburl);
        // 设置发起回调时请求body的值。
        callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
        // 设置发起回调请求的Content-Type。
        callback.setCalbackBodyType(Callback.CalbackBodyType.JSON);
        // 设置发起回调请求的自定义参数，由Key和Value组成，Key必须以x:开始。
        callback.addCallbackVar("x:objname", objName);
        putObjectRequest.setCallback(callback);

        PutObjectResult putObjectResult = client.putObject(putObjectRequest);
        // 读取上传回调返回的消息内容。
        byte[] buffer = new byte[1024];
        try {
            putObjectResult.getResponse().getContent().read(buffer);

            // 数据读取完成后，获取的流必须关闭，否则会造成连接泄漏，导致请求无连接可用，程序无法正常工作。
            putObjectResult.getResponse().getContent().close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //生成访问地址
        return createUrl(objName);
    }
    /**
     * 上传文件*/
    public static String upload(String objName,Date date, InputStream is){
        //上传文件到云存储中心
        client.putObject(bucket,objName,is);
        //生成访问地址
        return createUrl(objName,date);
    }
    /**
     * 验证对象名称是否存在*/
    public static boolean checkName(String objName){
        return client.doesObjectExist(bucket,objName);
    }
    /**
     * 删除文件*/
    public static boolean delObject(String objName){
        if(client.doesObjectExist(bucket,objName)){
            //sha
            client.deleteObject(bucket,objName);
            return true;
        }
        return false;
    }
    /**
     * 创建访问地址*/
    public static String createUrl(String objName){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.YEAR,10);
        return client.generatePresignedUrl(bucket,objName, calendar.getTime()).toString();
    }
    /**
     * 创建访问地址*/
    public static String createUrl(String objName, Date date){
        return client.generatePresignedUrl(bucket,objName, date).toString();
    }

}
